package com.supermarket.business.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.supermarket.business.domain.SupermarketProduct;
import com.supermarket.business.domain.SupermarketSalesDetail;
import com.supermarket.business.domain.SupermarketSalesOrder;
import com.supermarket.business.mapper.SupermarketProductMapper;
import com.supermarket.business.mapper.SupermarketSalesOrderMapper;
import com.supermarket.business.vo.SupermarketSalesDetailVO;
import com.supermarket.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supermarket.business.mapper.SupermarketSalesDetailMapper;
import com.supermarket.business.service.ISupermarketSalesDetailService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 销售明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-21
 */
@Service
public class SupermarketSalesDetailServiceImpl extends ServiceImpl<SupermarketSalesDetailMapper, SupermarketSalesDetail> implements ISupermarketSalesDetailService
{
    @Resource
    private SupermarketSalesDetailMapper supermarketSalesDetailMapper;
    @Resource
    private SupermarketSalesOrderMapper  supermarketSalesOrderMapper;
    @Resource
    private SupermarketProductMapper supermarketProductMapper;

    /**
     * 查询销售明细
     * 
     * @param detailId 销售明细主键
     * @return 销售明细
     */
    @Override
    public SupermarketSalesDetail selectSysSalesDetailByDetailId(Long detailId)
    {
        return supermarketSalesDetailMapper.selectSysSalesDetailByDetailId(detailId);
    }

    /**
     * 查询销售明细列表
     * 
     * @param supermarketSalesDetail 销售明细
     * @return 销售明细
     */
    @Override
    public List<SupermarketSalesDetail> selectSysSalesDetailList(SupermarketSalesDetail supermarketSalesDetail)
    {
        return supermarketSalesDetailMapper.selectSysSalesDetailList(supermarketSalesDetail);
    }

    /**
     * 新增销售明细
     * 
     * @param supermarketSalesDetail 销售明细
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysSalesDetail(SupermarketSalesDetail supermarketSalesDetail)
    {
        CheckOrderId(supermarketSalesDetail.getOrderId());
        //CheckProductId(supermarketSalesDetail.getProductId());

        //根据商品id查询商品名称并填写字段
        Long productId = Long.valueOf(supermarketSalesDetail.getProductName());
        supermarketSalesDetail.setProductId(productId);
        SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(productId);
        supermarketSalesDetail.setProductName(product.getProductName());
        supermarketSalesDetail.setUnitPrice(product.getPrice());
        if(product.getStock()<supermarketSalesDetail.getQuantity()){
            throw new IllegalArgumentException("商品:"+product.getProductName()+"库存不足");
        }
        //更新商品数量
        product.setStock(product.getStock()-supermarketSalesDetail.getQuantity());
        supermarketProductMapper.updateSysProduct(product);
        //计算金额小计
        BigDecimal unitPrice = supermarketSalesDetail.getUnitPrice();
        Long quantity = supermarketSalesDetail.getQuantity();
        BigDecimal amount = unitPrice.multiply(new BigDecimal(quantity));
        supermarketSalesDetail.setAmount(amount);

        return supermarketSalesDetailMapper.insertSysSalesDetail(supermarketSalesDetail);
    }

    /**
     * 修改销售明细
     * 
     * @param supermarketSalesDetail 销售明细
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysSalesDetail(SupermarketSalesDetail supermarketSalesDetail)
    {
        CheckOrderId(supermarketSalesDetail.getOrderId());
        if(supermarketSalesDetail.getProductId() == null){
            Long productId = Long.valueOf(supermarketSalesDetail.getProductName());
            SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(productId);
            supermarketSalesDetail.setProductName(product.getProductName());
            supermarketSalesDetail.setProductId(product.getProductId());
        }
        SupermarketProduct product2 = supermarketProductMapper.selectSysProductByProductId(supermarketSalesDetail.getProductId());
        if(!Objects.equals(product2.getProductName(),supermarketSalesDetail.getProductName())){
            SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(Long.valueOf(supermarketSalesDetail.getProductName()));
            supermarketSalesDetail.setProductName(product.getProductName());
            supermarketSalesDetail.setProductId(product.getProductId());
        }

        //根据商品id查询商品名称并填写字段
        SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(supermarketSalesDetail.getProductId());
        supermarketSalesDetail.setUnitPrice(product.getPrice());
        //回溯之前的商品库存
        SupermarketSalesDetail supermarketSalesDetail1 = supermarketSalesDetailMapper.selectSysSalesDetailByDetailId(supermarketSalesDetail.getDetailId());
        SupermarketProduct product1 = supermarketProductMapper.selectSysProductByProductId(supermarketSalesDetail1.getProductId());
        product1.setStock(product1.getStock()+supermarketSalesDetail1.getQuantity());
        supermarketProductMapper.updateSysProduct(product1);
        //更新商品数量
        Long quantity = supermarketSalesDetail.getQuantity();
        if(product.getStock()<quantity){
            throw new IllegalArgumentException("商品:"+product.getProductName()+"库存不足");
        }
        product.setStock(product.getStock() - quantity);
        supermarketProductMapper.updateSysProduct(product);

        //计算金额小计
        BigDecimal unitPrice = supermarketSalesDetail.getUnitPrice();
        BigDecimal amount = unitPrice.multiply(new BigDecimal(quantity));
        supermarketSalesDetail.setAmount(amount);

        return supermarketSalesDetailMapper.updateSysSalesDetail(supermarketSalesDetail);
    }

    /**
     * 批量删除销售明细
     * 
     * @param detailIds 需要删除的销售明细主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysSalesDetailByDetailIds(Long[] detailIds)
    {
        return supermarketSalesDetailMapper.deleteSysSalesDetailByDetailIds(detailIds);
    }

    /**
     * 删除销售明细信息
     * 
     * @param detailId 销售明细主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysSalesDetailByDetailId(Long detailId)
    {
        return supermarketSalesDetailMapper.deleteSysSalesDetailByDetailId(detailId);
    }

    @Override
    public List<SupermarketSalesDetailVO> selectSysSalesDetailListVO(SupermarketSalesDetail supermarketSalesDetail) {
        List<SupermarketSalesDetail> list = supermarketSalesDetailMapper.selectSysSalesDetailList(supermarketSalesDetail);
        List<SupermarketSalesDetailVO> listVO = new ArrayList<SupermarketSalesDetailVO>();
        for (SupermarketSalesDetail detail : list) {
            SupermarketSalesDetailVO vo = new SupermarketSalesDetailVO();
            BeanUtils.copyProperties(detail, vo);
            listVO.add(vo);
        }
        return listVO;
    }

    @Override
    public SupermarketSalesDetailVO selectSysSalesDetailVOByDetailId(Long detailId) {
        SupermarketSalesDetail detail = supermarketSalesDetailMapper.selectSysSalesDetailByDetailId(detailId);
        if (detail == null) {
            throw new IllegalArgumentException("销售明细不存在");
        }
        SupermarketSalesDetailVO vo = new SupermarketSalesDetailVO();
        BeanUtils.copyProperties(detail, vo);
        return vo;
    }

    private void CheckProductId(Long productId){
        SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(productId);
        if (product == null) {
            throw new IllegalArgumentException("该商品id不存在");
        }
    }

    private void CheckOrderId(Long orderId){
        SupermarketSalesOrder supermarketSalesOrder = supermarketSalesOrderMapper.selectSupermarketSalesOrderByOrderId(orderId);
        if (supermarketSalesOrder == null) {
            throw new IllegalArgumentException("该订单id不存在");
        }
    }
}
